package com.example.imtoken.modules.main.my.address.create

import androidx.lifecycle.MutableLiveData
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.http.BaseObserver
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.entitys.http.CoinsEntity
import com.example.imtoken.entitys.http.CoinsEntityItem
import com.example.imtoken.entitys.http.ResponseEntity
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.AddressBookSchema
import io.reactivex.schedulers.Schedulers

class CreateAddressViewModel : BaseViewModel() {

    private val coinsListLiveData = MutableLiveData<CoinsEntity>()

    private val coinsLiveData = MutableLiveData<CoinsEntityItem>()

    val isSubmit = MutableLiveData(false)

    fun getCoinsListData(): MutableLiveData<CoinsEntity> {
        return coinsListLiveData
    }

    fun getCoins(): MutableLiveData<CoinsEntityItem> {
        return coinsLiveData
    }

    val addressLiveData = MutableLiveData<String>()
    fun setAddressValue(value: String){
        addressLiveData.value = value
    }
    val nameLiveData = MutableLiveData<String>()
    val describeLiveData = MutableLiveData<String>()

    var _address: String? = null
        set(value) {
            field = value
            inspectEditText()
        }
    var _name: String? = null
        set(value) {
            field = value
            inspectEditText()
        }
    var _describe: String? = null
        set(value) {
            field = value
            inspectEditText()
        }

    var position = 0

    fun getCoinsList(coin: String? = null) {
        RetrofitUtils.apiService?.coins()
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.subscribe(object : BaseObserver<ResponseEntity<CoinsEntity>>() {
                override fun onSuccess(value: ResponseEntity<CoinsEntity>) {
                    if (value.code == 0) {
                        coinsListLiveData.postValue(value.data)
                        if (value.data != null) {
                            if (coin != null) {
                                for (i in 0 until value.data.size) {
                                    if (value.data[i].coin == coin) {
                                        coinsLiveData.postValue(value.data[i])
                                        coinsListLiveData.value?.get(i)?.isAddToken = 1
                                        position = i
                                    }
                                }
                            } else {
                                coinsLiveData.postValue(value.data[position])
                                coinsListLiveData.value?.get(position)?.isAddToken = 1
                            }
                        }
                    } else {
                        mErrorStr.postValue(value.msg)
                    }
                }

                override fun onFailed(e: Throwable) {

                }

            })
    }

    fun selectCoins(p: Int) {
        position = p
        val list = coinsListLiveData.value
        list?.forEach {
            it.isAddToken = 0
        }
        list?.get(position)?.isAddToken = 1
        coinsListLiveData.value = list
        coinsLiveData.value = list?.get(position)
    }

    private fun inspectEditText() {
        isSubmit.value = _address?.length ?: 0 > 0 && _name?.length ?: 0 > 0
    }

    fun addAddressBook(): Boolean {
        val addressBookSchema = AddressBookSchema(
            nameLiveData.value,
            coinsLiveData.value?.icon,
            coinsLiveData.value?.coin,
            addressLiveData.value,
            describeLiveData.value
        )
        return AppDataBase.instance.getAddressBookDao().insertAddressBook(addressBookSchema) > 0
    }

    fun updateAddressBook(id: Int): Boolean {
        val addressBookSchema = AddressBookSchema(
            nameLiveData.value,
            coinsLiveData.value?.icon,
            coinsLiveData.value?.coin,
            addressLiveData.value,
            describeLiveData.value,
            id
        )
        return AppDataBase.instance.getAddressBookDao().updateAddressBook(addressBookSchema) > 0
    }

    fun findOneAddressBook(id: Int) {
        val address = AppDataBase.instance.getAddressBookDao().findOneAddressBook(id)
        addressLiveData.value = address.address
        nameLiveData.value = address.name
        describeLiveData.value = address.description
        getCoinsList(address.coin)
    }

    fun deleteAddressBook(id: Int): Boolean {
        return AppDataBase.instance.getAddressBookDao().deleteAddressBook(id) > 0
    }

}